package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import viewInterfaces.IViewLoadTestResult;
import controllerInterfaces.IControllerLoadTestResult;

/**
 * Vista used to load previously saved test results.
 * 
 * @author Andy Cullache
 */
public class ViewLoadTestResult extends JFrame implements IViewLoadTestResult{

	private JPanel contentPane;
	private JTextField textField;
	private IControllerLoadTestResult myController;
	private String dirPath;
	private String nameLoad;

	/**
	 * Default constructor. Create the frame.
	 */
	public ViewLoadTestResult() {
		setResizable(false);
		
		setTitle("Load Test Result");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 114);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnChoose = new JButton("Choose");
		btnChoose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setDirectory();
			}
		});
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(readTestResult()){
					myController.updateTable();
					dispose();
				}
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnChoose)
							.addGap(33)
							.addComponent(textField, GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnCancel)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnChoose)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnOk)
						.addComponent(btnCancel))
					.addContainerGap())
		);
		getRootPane().setDefaultButton(btnOk);
		contentPane.setLayout(gl_contentPane);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}
	
	/**
	 * Sets the controller of this view.
	 * 
	 * @param myController      Benchmark panel controller.
	 */
	public void setController(IControllerLoadTestResult myController){
		this.myController = myController;
	}
	
	/**
	 * Method used to select the file to read and set the directory and file
	 *  name of the attribute "nameLoad" and "dirpath" respectively.
	 */
	private void setDirectory(){
		String[] aux = myController.selectDirectory();
		this.nameLoad = aux[0];
		this.dirPath = aux[1];
		this.textField.setText(this.dirPath+"\\"+this.nameLoad);
	}

	/**
	 * Method invokes the controller to read the CSV file results.
	 * 
	 * @return      True if the file can be read. False otherwise.
	 */
	private boolean readTestResult(){
		return this.myController.readTestResult(this.dirPath, this.nameLoad);
	}
	
	/**
	 * Method used to display error messages with the respective code.
	 * 
	 * @param code      Error code.
	 * @param message      Message code.
	 */
	public void renderError(int code, String message) {
		javax.swing.JOptionPane.showMessageDialog(this, code + " - " + message, "Error",javax.swing.JOptionPane.ERROR_MESSAGE);	
	}

	
}
